package com.fintech.third.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fintech.common.constant.Criteria;
import com.fintech.common.constant.ServiceException;
import com.fintech.common.entity.ResultData;
import com.fintech.common.enums.DateFormatEnum;
import com.fintech.common.enums.ResultCode;
import com.fintech.common.enums.UserInfoEnum;
import com.fintech.common.util.DateTimeUtil;
import com.fintech.common.util.RedisUtils;
import com.fintech.common.util.ShaUtil;
import com.fintech.third.constant.SmsTypeEnum;
import com.fintech.third.dao.RealNameAuthenticationMapper;
import com.fintech.third.dao.UserDao;
import com.fintech.third.service.sms.SmsService;
import com.fintech.user.entity.RealNameAuthentication;
import com.fintech.user.entity.User;
import com.fintech.user.service.UserService;
import com.marvels.common.utils.MarvelsLogUtil;

import net.sf.json.JSONObject;

@Service(value="userService")
public class UserServiceImpl implements UserService {
	@Autowired
	private UserDao userDao;
	@Autowired
	private RealNameAuthenticationMapper realNameAuthenticationMapper;
	@Autowired
	private SmsService smsService;
	
	@Override
	public ResultData<User> login(User user) {
		ResultData<User> resultData = new ResultData<User>();
		if(null==user || StringUtils.isEmpty(user.getMobileNo())){
			resultData.setResultCode(ResultCode.fail.getResultCode());
			resultData.setMessage("手机号不可为空！");
			return resultData;
		}
		
		if(StringUtils.isEmpty(user.getUserType())){
			resultData.setResultCode(ResultCode.fail.getResultCode());
			resultData.setMessage("用户类型不能为空!");
			return resultData;
		}
		
		User userInfo = null; 
		if("1".equals(user.getUserType())||"4".equals(user.getUserType()) ){
			//通过手机号查询用户信息
			userInfo = userDao.queryUserByMobileNo(user.getMobileNo(),"4");
		}
		
		if(userInfo==null){
			userInfo = userDao.queryUserByMobileNo(user.getMobileNo(),user.getUserType());
		}
		
		if(userInfo==null||StringUtils.isEmpty(userInfo.getMobileNo())){
			resultData.setResultCode(ResultCode.fail.getResultCode());
			resultData.setMessage("该手机号不存在 ！");
			return resultData;
		} 
		
		
		if(StringUtils.equals(userInfo.getUserType(),"1")){
			if(StringUtils.equalsIgnoreCase(userInfo.getStatus(), UserInfoEnum.userStatusApproveing.getCode())){
				resultData.setResultCode(ResultCode.fail.getResultCode());
				resultData.setMessage("该账号正在审批！");
				return resultData;
			}else if(StringUtils.equalsIgnoreCase(userInfo.getStatus(), UserInfoEnum.user_close_login.getCode())){
				resultData.setResultCode(ResultCode.fail.getResultCode());
				resultData.setMessage("该账号已关闭登录!");
				return resultData;
			}else if(StringUtils.equalsIgnoreCase(userInfo.getStatus(), UserInfoEnum.userStatusUNApprove.getCode())){
				resultData.setResultCode(ResultCode.fail.getResultCode());
				resultData.setMessage("该手机号未审批通过！");
				return resultData;
			}
		}else{
			//判断客户密码
			//ShaUtil.Encrypt(user.getPassWord(), "")
			if(StringUtils.isNotEmpty(user.getPassWord())&&
					!userInfo.getPassWord().equals(ShaUtil.Encrypt(user.getPassWord(), ""))){
				resultData.setResultCode(ResultCode.fail.getResultCode());
				resultData.setMessage("密码不正确 !");
				return resultData;
			}
		}
		/*if("3".equals(user.getUserType())){
			if(!user.getUserType().equals(userInfo.getUserType())){
				resultData.setResultCode(ResultCode.fail.getResultCode());
				resultData.setMessage("没有该助理");
				return resultData;
			}
		}*/
		
		resultData.setResultCode(ResultCode.success.getResultCode());
		resultData.setMessage("登录成功！");
		resultData.setData(userInfo);
		return resultData;
	}

	@Override
	public ResultData<User> loginV2(User user) {
		ResultData<User> resultData = new ResultData<User>();
		if(null==user || StringUtils.isEmpty(user.getMobileNo())){
			resultData.setResultCode(ResultCode.fail.getResultCode());
			resultData.setMessage("手机号不可为空！");
			return resultData;
		}
		
		if(StringUtils.isEmpty(user.getValidCode())){
			resultData.setResultCode(ResultCode.fail.getResultCode());
			resultData.setMessage("验证码不可为空！");
			return resultData;
		}
			
		if(StringUtils.isEmpty(user.getUserType())){
			resultData.setResultCode(ResultCode.fail.getResultCode());
			resultData.setMessage("用户类型不能为空!");
			return resultData;
		}
		
		if (!smsService.checkCode(user.getMobileNo(), user.getValidCode(), SmsTypeEnum.LOGIN.getName())) {
			resultData.setResultCode(ResultCode.fail.getResultCode());
			resultData.setMessage("验证码错误！");
			return resultData;
		}
		
		User userInfo = null; 
		Integer operateFlag = 0;
		if("1".equals(user.getUserType())||"4".equals(user.getUserType()) ){
			//通过手机号查询用户信息
			userInfo = userDao.queryUserByMobileNo(user.getMobileNo(),"4");
		}
		
		if(userInfo==null){
			userInfo = userDao.queryUserByMobileNo(user.getMobileNo(),user.getUserType());
		}
		
		// 如果账号不存在 静默注册
		if(userInfo==null || StringUtils.isEmpty(userInfo.getMobileNo())){
			ResultData<User> resultUser = this.clientRegisterUser(user);
			if(resultUser.getResultCode().equals(ResultCode.success.getResultCode())){
				userInfo = resultUser.getData();
				operateFlag = 1; // 注册登录
			}else{
				return resultUser;
			}
		} 
		
		if(StringUtils.equals(userInfo.getUserType(),"1")){
			if(StringUtils.equalsIgnoreCase(userInfo.getStatus(), UserInfoEnum.userStatusApproveing.getCode())){
				resultData.setResultCode(ResultCode.fail.getResultCode());
				resultData.setMessage("该账号正在审批！");
				return resultData;
			}else if(StringUtils.equalsIgnoreCase(userInfo.getStatus(), UserInfoEnum.user_close_login.getCode())){
				resultData.setResultCode(ResultCode.fail.getResultCode());
				resultData.setMessage("该账号已关闭登录!");
				return resultData;
			}else if(StringUtils.equalsIgnoreCase(userInfo.getStatus(), UserInfoEnum.userStatusUNApprove.getCode())){
				resultData.setResultCode(ResultCode.fail.getResultCode());
				resultData.setMessage("该手机号未审批通过！");
				return resultData;
			}
		}
		// 因为登录注册整合成一个接口，所以需要告诉前端这次是登录操作还是注册并登录操作
		if(userInfo != null){
			userInfo.setOperateFlag(operateFlag);	
		}
		userInfo.setPassWord(null);
		userInfo.setCreatedBy(null);
		userInfo.setUpdatedBy(null);
//		userInfo.setId(null);
		resultData.setResultCode(ResultCode.success.getResultCode());
		resultData.setMessage("登录成功！");
		resultData.setData(userInfo);
		return resultData;
	}
	
	@Override
	public User query_by_phone(String username) {
		return userDao.query_by_phone(username);
	}

	@Override
	public int updatePwd(User user) {
		user.setPassWord(ShaUtil.Encrypt(user.getPassWord(), ""));
		return userDao.updatePwd(user);
	}
	@Deprecated
	@Override
	public List<User> queryOrgByLiveId(Integer level, Integer levelId) {
		List<User> userList = userDao.queryByLevelAndLevelId(level,levelId);
		return userList;
	}


	@Override
	public ResultData<User> registerUser(User user) {
		ResultData<User> resultData = new ResultData<User>();
		
		if(null==user || StringUtils.isEmpty(user.getMobileNo())
				|| StringUtils.isEmpty(user.getUserType())
				|| StringUtils.isEmpty(user.getUserType())){
			resultData.setResultCode(ResultCode.fail.getResultCode());
			resultData.setMessage("注册手机号不可为空！");
			return resultData;
		}
		
		//通过手机号查询用户
		User userInfo = userDao.queryUserByMobileNo(user.getMobileNo(),"1");
		if(null!=userInfo && StringUtils.isNotEmpty(userInfo.getMobileNo())&&
				UserInfoEnum.userRegsiterStep2.getCode().equals(userInfo.getRegistStep())&&
				UserInfoEnum.userStatusUNApprove.getCode().equals(userInfo.getStatus()) ){
			resultData.setResultCode(ResultCode.success.getResultCode());
			resultData.setData(userInfo);
			return resultData;
		}
		
		
		if(null!=userInfo && StringUtils.isNotEmpty(userInfo.getMobileNo())&&
				UserInfoEnum.userRegsiterStep2.getCode().equals(userInfo.getRegistStep())&&
				UserInfoEnum.userStatusApprove.getCode().equals(userInfo.getStatus()) ){
			resultData.setResultCode(ResultCode.fail.getResultCode());
			resultData.setMessage("该手机号已注册,并且已经身份认证通过!");
			resultData.setData(userInfo);
			return resultData;
		}
		
		
		if(null!=userInfo && StringUtils.isNotEmpty(userInfo.getMobileNo())&&UserInfoEnum.userRegsiterStep1.getCode().equals(userInfo.getRegistStep()) ){
			resultData.setResultCode(ResultCode.success.getResultCode());
			resultData.setMessage("该手机号已注册,但未身份认证！");
			resultData.setData(userInfo);
			return resultData;
		}
		
		
		//设置状态为未绑定
		user.setStatus(UserInfoEnum.userStatusUnbind.getCode());
		//设置步骤为1
		user.setRegistStep(UserInfoEnum.userRegsiterStep1.getCode());
		
		if(StringUtils.isEmpty(user.getUserName())){
			user.setUserName(user.getMobileNo());
		}
		if(StringUtils.isNotEmpty(user.getPassWord())){
			user.setPassWord(ShaUtil.Encrypt(user.getPassWord(), ""));
		}else{
			user.setPassWord("");
		}
		if(StringUtils.isEmpty(user.getNickName())){
			user.setNickName(user.getMobileNo());
		}
		if(StringUtils.isEmpty(user.getUserPhotoUrl())){
			user.setUserPhotoUrl("https://www.jd.com/favicon.ico");		
		}
		//新增用户信息
		try {
			userDao.insertUser(user);
		} catch (Exception e) {
			resultData.setMessage("注册失败!");
			resultData.setResultCode(ResultCode.fail.getResultCode());
			e.printStackTrace();
			return resultData;
		}
		
		resultData.setResultCode(ResultCode.success.getResultCode());
		resultData.setMessage(ResultCode.success.getMessage());
		resultData.setData(userDao.queryUserByMobileNo(user.getMobileNo(),user.getUserType()));
		
		return resultData;
	}
	
	
	@Override
	public ResultData<User> clientRegisterUser(User user) {
		ResultData<User> resultData = new ResultData<User>();
		if(null==user || StringUtils.isEmpty(user.getMobileNo())
				|| StringUtils.isEmpty(user.getUserType())){
			resultData.setResultCode(ResultCode.fail.getResultCode());
			resultData.setMessage("信息不完整!");
			return resultData;
		}
		User userDb= userDao.queryUserByMobileNo(user.getMobileNo(),user.getUserType());
		if(userDb!=null){
			resultData.setResultCode(ResultCode.fail.getResultCode());
			resultData.setMessage("该手机号码已经注册!");
			return resultData;
		}
		//设置状态为未绑定
		user.setStatus(UserInfoEnum.userStatusUnbind.getCode());
		//设置步骤为1
		user.setRegistStep(UserInfoEnum.userRegsiterStep1.getCode());
		
		if(StringUtils.isEmpty(user.getUserName())){
			user.setUserName(user.getMobileNo());
		}
		if(StringUtils.isNotEmpty(user.getPassWord())){
			user.setPassWord(ShaUtil.Encrypt(user.getPassWord(), ""));
		}else{
			user.setPassWord(ShaUtil.Encrypt(user.getMobileNo().substring(5, 11), ""));
		}
		
		if(StringUtils.isEmpty(user.getNickName())){
			user.setNickName(user.getMobileNo());
		}
		if(StringUtils.isEmpty(user.getUserPhotoUrl())){
			user.setUserPhotoUrl("https://www.jd.com/favicon.ico");		
		}
		//新增用户信息
		try {
			userDao.insertUser(user);
		} catch (Exception e) {
			resultData.setMessage("注册失败!");
			resultData.setResultCode(ResultCode.fail.getResultCode());
			MarvelsLogUtil.getInstance().error(user.getMobileNo() + "注册失败", e);
			return resultData;
		}
		resultData.setResultCode(ResultCode.success.getResultCode());
		resultData.setMessage(ResultCode.success.getMessage());
		resultData.setData(userDao.queryUserByMobileNo(user.getMobileNo(),user.getUserType()));
		return resultData;
	}

	@Override
	public ResultData<User> modifyUser(User user) {
		ResultData<User> resultData = new ResultData<User>();
		
		if(null==user || StringUtils.isEmpty(user.getIdFintechUmUser())){
			resultData.setResultCode(ResultCode.fail.getResultCode());
			resultData.setMessage(ResultCode.fail.getMessage());
			return resultData;
		}
		
		try {
			userDao.modifyUserInfoById(user);
		} catch (Exception e) {
			e.printStackTrace();
			MarvelsLogUtil.getInstance().error(e.getMessage());
			resultData.setResultCode(ResultCode.fail.getResultCode());
			resultData.setMessage(ResultCode.fail.getMessage());
			return resultData;
		}
		User userInfo = userDao.queryUserByMobileNo(user.getIdFintechUmUser(),user.getUserType());
		
		resultData.setResultCode(ResultCode.success.getResultCode());
		resultData.setMessage(ResultCode.success.getMessage());
		resultData.setData(userInfo);
		
		return resultData;
	}

	@Override
	public ResultData<Boolean> validToken(String mobileNo, String token) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultData<Boolean> validAccountRegister(String mobileNo,String userType) {
		ResultData<Boolean> resultData = new ResultData<Boolean>();
		
		if(StringUtils.isEmpty(mobileNo)){
			resultData.setResultCode(ResultCode.fail.getResultCode());
			resultData.setMessage("手机号不可为空！");
			return resultData;
		}
		if(StringUtils.isEmpty(userType)){
			resultData.setResultCode(ResultCode.fail.getResultCode());
			resultData.setMessage("用户类型不能为空!");
			return resultData;
		}
		
		//通过手机号查询用户信息
		User userInfo = userDao.queryUserByMobileNo(mobileNo,userType);
		if(userInfo==null){  //如果等于空可以注册
			resultData.setData(true);
			return resultData;
		}
		//如果是客户经理注册了但是未绑定身份证任然返回true
		if(userType.equals("1")&&UserInfoEnum.userRegsiterStep1.getCode().equals(userInfo.getRegistStep())){
			resultData.setData(true);
			return resultData;
		}
		//客户经理注册也实名认证了但是审核拒绝了返回t
		if(userType.equals("1")&&UserInfoEnum.userRegsiterStep2.getCode().equals(userInfo.getRegistStep())
				&&StringUtils.equals(UserInfoEnum.userStatusUNApprove.getCode(), userInfo.getStatus()) ){
			resultData.setData(true);
			return resultData;
		}
		
		
		/*if(null!= userInfo && StringUtils.isNotEmpty(userInfo.getMobileNo())){
			resultData.setResultCode(ResultCode.fail.getResultCode());
			resultData.setMessage("手机号已存在！");
			resultData.setData(userInfo);
			return resultData;
		}		
		resultData.setData(userInfo);
		resultData.setResultCode(ResultCode.success.getResultCode());
		resultData.setMessage(ResultCode.success.getMessage());
		MarvelsLogUtil.getInstance().info("resultData============>>"+JSONObject.fromObject(resultData)+"");*/
		resultData.setData(false);
		return resultData;
	}

	@Override
	public ResultData<Boolean> logout(String mobileNo, String token) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public User queryUserByMobileNo(String mobileNo,String userType) {
		return userDao.queryUserByMobileNo(mobileNo,userType);
	}
	
	@Override
	public User queryByMobileNo(String mobileNo) {
		return userDao.queryByMobileNo(mobileNo);
	}

	@Override
	public ResultData<User> validLogin(User user) {
		ResultData<User> resultData = new ResultData<User>();
		if(null==user || StringUtils.isEmpty(user.getMobileNo())){
			resultData.setResultCode(ResultCode.fail.getResultCode());
			resultData.setMessage("手机号不可为空！");
			return resultData;
		}
		
		//通过手机号查询用户信息
		User userInfo = userDao.queryUserByMobileNo(user.getMobileNo(),"4");
		if(userInfo==null){
			userInfo = userDao.queryUserByMobileNo(user.getMobileNo(),"1");
		}
		if(null==userInfo ){
			resultData.setResultCode(ResultCode.fail.getResultCode());
			resultData.setMessage("手机号未注册，请先注册！");
			return resultData;
		}
		if(StringUtils.equalsIgnoreCase(userInfo.getRegistStep(), UserInfoEnum.userRegsiterStep1.getCode()) ){
			resultData.setMessage("请完善注册资料！");
			return resultData;
		}
		
		if(StringUtils.equalsIgnoreCase(userInfo.getStatus(), UserInfoEnum.userStatusApproveing.getCode())){
			resultData.setMessage("该账号正在审批！");
			return resultData;
		}else if(StringUtils.equalsIgnoreCase(userInfo.getStatus(), UserInfoEnum.user_close_login.getCode())){
			resultData.setMessage("该账号已关闭登录!");
			return resultData;
		}else if(StringUtils.equalsIgnoreCase(userInfo.getStatus(), UserInfoEnum.userStatusUNApprove.getCode())){
			resultData.setMessage("该手机号未审批通过！");
			return resultData;
		}
		resultData.setResultCode(ResultCode.success.getResultCode());
		resultData.setMessage(ResultCode.success.getMessage());
		resultData.setData(userInfo);
		return resultData;
	}

	@Override
	public Map<String, Object> orgIdByUserId(String userId) {
		return userDao.orgIdByUserId(userId);
	}


	@Override
	public User queryUserById(Integer id) {
		return userDao.queryUserById(id);
	}

	@Override
	public Integer queryCount(Criteria<User> param) {
		return userDao.queryCount(param);
	}


	@Override
	public User queryByOpenId(String openId) {
		return userDao.queryByOpenId(openId);
	}


	@Override
	public List<User> findCustManager(Map<String, Object> param) {
		return userDao.findCustManager(param);
	}


	@Override
	public Integer findCustManagerCount(Map<String, Object> param) {
		return userDao.findCustManagerCount(param);
	}


	@Override
	public ResultData<String> updateUserStatus(User userEntity) {
		ResultData<String> result = new ResultData<String>();
		try {
			
			User user = new User();
			User tempUser = userDao.queryUserById(userEntity.getId());
			user.setId(userEntity.getId());
			user.setIdFintechUmUser(tempUser.getIdFintechUmUser());
			user.setStatus(userEntity.getStatus());
			RedisUtils.getInstance().del(tempUser.getIdFintechUmUser());
			user.setAuditName(userEntity.getAuditName());
			userDao.modifyUserInfoById(user);
		} catch (Exception e) {
			result.setErrorMessage("更新状态失败");
			e.printStackTrace();
		}
		return result;
	}


	@Override
	public void modifyUserMobile(String mobileNum, String idFintechUmUser) {
		User user = new User();
		user.setIdFintechUmUser(idFintechUmUser);
		user.setMobileNo(mobileNum);
		user.setNickName(mobileNum);
		user.setUserName(mobileNum);
		userDao.modifyUserInfoById(user);
	}


	@Override
	public void deleteUserByUserId(Integer userId) {
		userDao.deleteUserByUserId(userId);
	}
	
	@Override
	public void cancelUserByUserId(Integer userId, String reason) {
		userDao.cancelUserByUserId(userId, reason);
	}


	@Override
	public User queryByIdCard(String idCard) {
		return userDao.queryByIdCard(idCard);
	}

	@Override
	public User queryByIdCardAndName(String idCard,String name){
		return userDao.queryByIdCardAndName(idCard, name);
	}	

	@Override
	public User selectByOrgId(Integer orgId) {
		return userDao.selectByOrgId(orgId);
	}
	
	@Override
	public void updateByOrgId(Criteria<User> param,String typeId) {
		if(null==param){
			return;
		}
		String orgId = param.getOtherParam().get("orgId").toString();
		List<User> userList = new ArrayList<User>();
		if(typeId.equals("1")){//开通登录+关闭登录的话要级联处理
			userList = userDao.selectBranchByOrgId(Integer.valueOf(orgId));
		}else{
			User tempUser = userDao.selectByOrgId(Integer.valueOf(orgId));
			userList.add(tempUser);
		}
		/*if(param.getParam().getStatus().equals("3")||param.getParam().getStatus().equals("-3")){//开通登录+关闭登录的话要级联处理
			userList = userDao.selectBranchByOrgId(Integer.valueOf(orgId));
		}else{
			User tempUser = userDao.selectByOrgId(Integer.valueOf(orgId));
			userList.add(tempUser);
		}*/
		List<Integer> userIds = new ArrayList<Integer>();
		Map<String,Object> otherParam = new HashMap<String, Object>();
		otherParam.put("orgId", orgId);
		if(null==userList||userList.size()==0)return;
		for (User user : userList) {
			userIds.add(user.getId());
			RedisUtils.getInstance().del(user.getIdFintechUmUser());
		}
		otherParam.put("userIds", userIds);
		param.setOtherParam(otherParam);
		userDao.updateByOrgId(param);
	}

	@Override
	public User regionUserLogin(User user) {
		User userDb = userDao.queryUserByMobileNo(user.getMobileNo(), "4");
		return userDb;
	}
	
	@Override
	public List<User> selectByChannelCode(String channelCode) {
		return userDao.selectByChannelCode(channelCode);
	}


	@Override
	public User selectByCondition(Map<String, Object> dataMap) {
		return userDao.selectByCondition(dataMap);
	}


	@Override
	public int updateIsMessagePush(Integer id,boolean isMessagePush) {
		return userDao.updateIsMessagePush(id,isMessagePush);
	}


	@Override
	public boolean selectUserMessageInstall(Integer userId) {
		return userDao.selectUserMessageInstall(userId);
	}


	@Override
	public int saveUser(User user) {
		
		return userDao.insertUser(user);
	}


	@Override
	public Integer singleStepRegister(Map<String, String> param) throws ServiceException{
		MarvelsLogUtil.getInstance().info("singleStepRegister==>>>>"+JSONObject.fromObject(param));
		Integer userId=null;
		try {
			String channelType = param.get("channelType");
			if(StringUtils.isEmpty(channelType)){
				channelType = "A4111a200201";
			}
			
			User user = new User();
			user.setUserName(param.get("userName"));
			String passWord = param.get("passWord");
			user.setPassWord(ShaUtil.Encrypt(passWord, ""));
			user.setNickName(param.get("nickName"));
			user.setRealName(param.get("realName"));
			user.setUserType("2");
			user.setChannelCode(channelType);
			user.setMobileNo(param.get("mobileNo"));
			user.setStatus("1");
			if(StringUtils.isEmpty(param.get("userPhotoUrl"))){
				user.setUserPhotoUrl("https://www.jd.com/favicon.ico");		
			}
			user.setRegistStep("1");
			userDao.insertUser(user);
			
			RealNameAuthentication realNameAuthentication = new RealNameAuthentication();
			userId = user.getId();
			Date date = new Date();
			realNameAuthentication.setUserId(userId);
			realNameAuthentication.setIdentityCardNo(param.get("identityCardNo"));
			realNameAuthentication.setRealName(param.get("realName"));
			realNameAuthentication.setAddress(param.get("address"));
			realNameAuthentication.setNation(param.get("nation"));
			String sexStr = param.get("sex").toString();
			Integer sex = 0;
			if(!sexStr.equals("女")){
				sex = 1;
			}
			realNameAuthentication.setSex(sex);
			String idCardImg1 = param.get("idCardImg1");
			String authority = param.get("authority");
			String idCardImg2 = param.get("idCardImg2");
			String photo = param.get("photo");
			realNameAuthentication.setAuthority(authority == null ? "" : authority);
			realNameAuthentication.setValidityfrom(DateTimeUtil.formatStrDate(DateFormatEnum.yyyy_MM_dd_HH_mm_ss, param.get("validityFrom")));
			realNameAuthentication.setValidityto(DateTimeUtil.formatStrDate(DateFormatEnum.yyyy_MM_dd_HH_mm_ss, param.get("validityTo")));
			realNameAuthentication.setIdcardimg1(idCardImg1 == null ? "" : idCardImg1);
			realNameAuthentication.setIdcardimg2(idCardImg2 == null ? "" : idCardImg2);
			realNameAuthentication.setPhoto(photo == null ? "" : photo);
			realNameAuthentication.setSmallidcardimg1(param.get("smallIdCardImg1"));
			realNameAuthentication.setSmallidcardimg2(param.get("smallIdCardImg2"));
			realNameAuthentication.setSmallphoto(param.get("smallPhoto"));
			realNameAuthentication.setCreatedBy(userId.toString());
			realNameAuthentication.setCreatedDate(date);
			realNameAuthentication.setUpdatedBy(userId.toString());
			realNameAuthentication.setUpdatedDate(date);
			realNameAuthenticationMapper.insertSelective(realNameAuthentication);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("singleStepRegister>>>>>>",e);
			throw new ServiceException(e.getMessage());
		}
		return userId;
	}


	@Override
	public User queryById(Integer id) {
		return userDao.queryById(id);
	}
	
	@Override
	public boolean validateDevice(User user) {
		List<User> userList = userDao.queryByDeviceId(user.getDeviceId());
		if (userList != null && userList.size() > 0) {
			for (User u : userList) {
				if (!u.getMobileNo().equals(user.getMobileNo())) {
					return false;
				}
			}
		}
		return true;
	}
	@Override
	public RealNameAuthentication findRealNameCount(User user) {
		return userDao.findRealNameCount(user);
	}

	@Override
	public int insertGuideInfo(User user) {
		return userDao.insertGuideInfo(user);
	}

	@Override
	public int updateGuideInfo(User user) {
		return userDao.updateGuideInfo(user);
	}

	   
	/**
	 * 根据手机号码查询用户信息，不排除无效客户
	 * 
	 * @param mobileNo
	 * @return
	 */
	@Override
	public User queryAllUserByMobileNo(String mobileNo){
		return userDao.queryAllUserByMobileNo(mobileNo);
	}
	/**
 	 * 查询用户是否为存量用户
 	 * @param selectType 查询方式
 	 * @param phone
 	 * @param cid
 	 * @param name
 	 * @return
 	 */
	@Override
 	public Integer queryStockUser(String selectType,String phone,String cid,String name){
 		return realNameAuthenticationMapper.queryStockUser(selectType, phone, cid, name);
 	}
}
